package com.mantru.iotpot.dishes

import android.app.Application
import android.util.Log
import android.widget.Toast
import androidx.databinding.ObservableField
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mantru.iotpot.ApplicationClass
import com.mantru.iotpot.Bean.DishesListBean
import com.mantru.iotpot.Util.ToastUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject

class DishesListViewModel(application: Application) : AndroidViewModel(application) {

    //实现和activity_dishes.xml的双向绑定
    //菜谱名文本框内容
    val dishName = ObservableField<String>("")
    // 数据集合
    val numList = ObservableField<MutableList<String>>()
    val weightList = ObservableField<MutableList<String>>()
    private val _dishesList = MutableLiveData<MutableList<DishesListBean>>()
    val dishesList: LiveData<MutableList<DishesListBean>> = _dishesList
    // 添加一个 LiveData 来通知 Activity 网络请求的结果
    private val _vegetableExistsResult = MutableLiveData<Boolean>()
    val vegetableExistsResult: LiveData<Boolean> = _vegetableExistsResult

    private val applicationClass: ApplicationClass = application as ApplicationClass

    //交互参数
    var name = ""
    //添加一个用于记录当前规格数量额参数
    var askLength = 0

    // 初始化viewModel
    init {
        numList.set(mutableListOf())
        weightList.set(mutableListOf())
    }

    // 添加新项
    fun addItem() {
        numList.get()?.add("")
        weightList.get()?.add("")
        updateDishesList()
    }

    // 删除项
    fun deleteItem(position: Int) {
        numList.get()?.removeAt(position)
        weightList.get()?.removeAt(position)
        updateDishesList()
    }

    // 更新菜品列表数据
    private fun updateDishesList() {
        //确保在主线程进行
        viewModelScope.launch(Dispatchers.Main) {
            val newList = mutableListOf<DishesListBean>()
            if (numList.get()?.isNotEmpty() == true){
                for ( i in 0 until numList.get()!!.size){
                    newList.add(
                        DishesListBean(
                            num = numList.get()?.get(i).toString(),
                            weight = weightList.get()?.get(i).toString(),
                        )
                    )
                }
            }
            _dishesList.postValue(newList)
        }
    }

    //判断是否每一项的份数和重量是否为空，如果为空则不能跳转
    fun judgeNextOrNot(): Boolean {
        if (dishName.get()?.isEmpty() == true){
            //Toast.makeText(applicationClass, "请输入您的菜谱名", Toast.LENGTH_SHORT).show()
            ToastUtils.show(applicationClass, "请输入您的菜谱名")
            return false
        }
        if ( numList.get()?.contains("") == true ){
            //Toast.makeText(applicationClass, "请完成您选择规格的份量", Toast.LENGTH_SHORT).show()
            ToastUtils.show(applicationClass, "请完成您选择规格的份量")
            return false
        }
        if ( weightList.get()?.contains("") == true ){
            //Toast.makeText(applicationClass, "请完成您选择规格的重量", Toast.LENGTH_SHORT).show()
            ToastUtils.show(applicationClass, "请完成您选择规格的重量")
            return false
        }
        if ( weightList.get()?.contains("0") == true ){
            //Toast.makeText(applicationClass, "请完成您选择规格的重量", Toast.LENGTH_SHORT).show()
            ToastUtils.show(applicationClass, "菜谱重量不能为0")
            return false
        }
        return true
    }

    //获取网络数据
    fun getHttpValue(api: String, map: Map<String, Any>){
        when ( api ){
            "GetSQLData_VegetableAndSpec_IsExists" ->{//判断菜谱名是否存在
                val jsonData = applicationClass.GetSQLData_VegetableAndSpec_IsExists(map["vegetable_id"] as Int, map["vegetable_name"] as String, map["copies_qty"] as Int)
                if ( jsonData ){
                    _vegetableExistsResult.postValue(true) // 通知Activity
                } else {
                    _vegetableExistsResult.postValue(false) // 通知Activity
                }
            }
        }
    }
}